package kgame5.k2d {
import kgame5.k3d.core.math.Vector3D;
import kgame5.kmath.MathDG;

import flash.display.DisplayObject;
import flash.geom.Rectangle;

/**
	 * @author max
	 * 2010-12-14
	 * 25D世界里的摄像机
	 * 
	 * --------X
	 * |
	 * |
	 * |
	 * y
	 */
	public class Camera2D {
		
		private var viewRect:Rectangle;//世界的视窗
		
		public var target:Vector3D;//摄像机盯住的目标
		
		private var mc_world:DisplayObject;//由外部传入，裁剪用
		
		//世界区域，保证摄像机不出边界
		private var worldWidth:int;
		private var worldHeight:int;
		
		public function Camera2D(mc_world:DisplayObject){
			viewRect=new Rectangle(0,0,400,400);
			target=new Vector3D();
			this.mc_world=mc_world;
		}
		
		public function setViewSize(w:int,h:int):void{
			viewRect.width=w;
			viewRect.height=h;
		}

		public function setWorldSize(w:int,h:int):void{
			worldWidth =w;
			worldHeight=h;
		}

		
		
//		//是否可以裁剪掉了
//		public function canCull(mc:DisplayObject):Boolean{
//			return mc.getBounds(mc_world).intersection(viewRect)==null;
//		}
		
		
		
		//更新了target,更新viewRect,注意这个要在world之前执行
		public function update():void{
			//让摄像机不出边界
			
			target.x=MathDG.Clamp(target.x, viewRect.width*0.5, worldWidth-viewRect.width*0.5);
			target.y=MathDG.Clamp(target.y, viewRect.height*0.5, worldHeight-viewRect.height*0.5);
			
			//设定viewRect
			viewRect.x=target.x-viewRect.width*0.5;
			viewRect.y=target.y-viewRect.height*0.5;
			
			mc_world.scrollRect=viewRect;
		}
	}
}
